package com.view;

import com.controller.WindowFrame;
import com.panel.MyPanel;
import com.util.GameUtil;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * @author: Combo
 * @version: 1.0
 * @date: 2020/12/29
 * @description: 游戏主窗口
 */
public class MainFrame extends JFrame implements MouseListener {

    // 开始 帮助 取消按钮
    JLabel start;
    JLabel help;
    JLabel exit;

    /**
     * 无参构造,创建时调用
     */
    public MainFrame() {
        // 开始按钮
        start = new JLabel(GameUtil.getIcon("images/start.png"));
        start.setBounds(350, 320, 150 ,40);
        start.setEnabled(false); // 按钮为灰色
        start.addMouseListener(this);
        this.add(start);

        // 帮助按钮
        help = new JLabel(GameUtil.getIcon("images/help.png"));
        help.setBounds(350, 420, 150 ,40);
        help.setEnabled(false); // 按钮为灰色
        help.addMouseListener(this);
        this.add(help);

        // 取消按钮
        exit = new JLabel(GameUtil.getIcon("images/exit.png"));
        exit.setBounds(350, 520, 150 ,40);
        exit.setEnabled(false); // 按钮为灰色
        exit.addMouseListener(this);
        this.add(exit);

        MyPanel mainPanel = new MyPanel("images/main.png", 0, 0, 1200, 730);
        this.add(mainPanel);

        this.setSize(1200, 730);
        this.setLocationRelativeTo(null);
        this.setUndecorated(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setIconImage(GameUtil.getImage("images/logo.png"));
        this.setVisible(true);
    }

    /**
     * 鼠标点击后事件
     *
     * @param e
     */
    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource().equals(start)){
            // 跳转下一个界面
            new WindowFrame().start();
            dispose();
        } else if (e.getSource().equals(help)){
            JOptionPane.showMessageDialog(null, "O(∩_∩)O哈哈~");
        } else if (e.getSource().equals(exit)){
           dispose();
        }
    }

    /**
     * Invoked when a mouse button has been pressed on a component.
     *
     * @param e
     */
    @Override
    public void mousePressed(MouseEvent e) {

    }

    /**
     * Invoked when a mouse button has been released on a component.
     *
     * @param e
     */
    @Override
    public void mouseReleased(MouseEvent e) {

    }

    /**
     * Invoked when the mouse enters a component.
     * 鼠标hover事件
     * 当鼠标在按钮上时
     * @param e
     */
    @Override
    public void mouseEntered(MouseEvent e) {
        // 当鼠标移动至开始按钮上
        if (e.getSource().equals(start)){
            start.setEnabled(true);
        } else if (e.getSource().equals(help)){
            help.setEnabled(true);
        } else if (e.getSource().equals(exit)){
            exit.setEnabled(true);
        }
    }

    /**
     * Invoked when the mouse exits a component.
     * 鼠标hover事件
     * 当鼠标移出按钮
     * @param e
     */
    @Override
    public void mouseExited(MouseEvent e) {
        if (e.getSource().equals(start)){
            start.setEnabled(false);
        } else if (e.getSource().equals(help)){
            help.setEnabled(false);
        } else if (e.getSource().equals(exit)){
            exit.setEnabled(false);
        }
    }
}
